<?php
require('../configs/include.php');
require(C_FULL_PATH . 'modules/m_phpass/PasswordHash.php');

class c_consultar_videojuego extends super_controller {

    public function SeleccionarVidejuego(){
        $options['videojuego']['lvl2'] = "all";
        $this->orm->connect();
        $this->orm->read_data(array("videojuego"), $options);
        $videojuego = $this->orm->get_objects("videojuego");
        $this->orm->close();
        $this->engine->assign('videojuego', $videojuego);         
    }
          
    public function buscar(){       
        $cod['videojuego']['id'] = $_POST["id"];
        $options['videojuego']['lvl2'] = "one";
        
        $this->orm->connect();
        $this->orm->read_data(array("videojuego"), $options, $cod);
        $videojuego = $this->orm->get_objects("videojuego");
        $this->orm->close();
        $this->engine->assign('videojuego_sel', $videojuego);    
        $this->engine->assign('id_videojuego', $videojuego[0]->get('id')); 
        $this->personajes($_POST["id"]);
        $this->niveles($_POST["id"]);
        $this->sonidos($_POST["id"]);
        $this->historias($_POST["id"]);
    }
        
    public function personajes($id){
        echo $id;
        $cod['personaje']['videojuego'] = $id;
        $options['personaje']['lvl2'] = "videojuego";
        
        $this->orm->connect();
        $this->orm->read_data(array("personaje"), $options, $cod);
        $personaje = $this->orm->get_objects("personaje");
        $this->orm->close();
        $this->engine->assign('personaje', $personaje);    
        
        
    }
          
    public function niveles($id){
        $cod['nivel']['videojuego'] = $id;
        $options['nivel']['lvl2'] = "videojuego";
        
        $this->orm->connect();
        $this->orm->read_data(array("nivel"), $options, $cod);
        $nivel = $this->orm->get_objects("nivel");
        $this->orm->close();
        $this->engine->assign('nivel', $nivel);  
    }
         
    public function sonidos($id){
        $cod['sonido']['videojuego'] = $id;
        $options['sonido']['lvl2'] = "videojuego";
        $this->orm->connect();
        $this->orm->read_data(array("sonido"), $options, $cod);
        $sonido = $this->orm->get_objects("sonido");
        $this->orm->close();
        $this->engine->assign('sonido', $sonido);     
    }
            
    public function historias($id){
        
        $cod['historia']['videojuego'] = $id;
        $options['historia']['lvl2'] = "videojuego";
        $this->orm->connect();
        $this->orm->read_data(array("historia"), $options, $cod);
        $historia = $this->orm->get_objects("historia");
        $this->orm->close();
        $this->engine->assign('historia', $historia);
    }
       public function editar(){
             $videojuego = new videojuego($this->post);
            $options = "update";
                           
            $this->orm->connect();
            $this->orm->update_data($options, $videojuego);
            $this->orm->close();

            $this->type_warning = "success";
            $this->msg_warning = "videojuego actualizado correctamente!";
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
          }
   
    public function display() {
        $this->SeleccionarVidejuego();

        $this->engine->assign('title', 'Editar videojuego');
        $this->engine->display('header.tpl');
         if ($this->error == 1) {
            $this->engine->assign('obj', $this->post);
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
        }
        $this->engine->display($this->temp_aux);
        $this->engine->display('editar_videojuego.tpl');
        $this->engine->display('footer.tpl');
    }

    public function run() {
        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
        }
        $this->display();
    }

}

$call = new c_consultar_videojuego();
$call->run();
?>
